package cn.kgc.service.impl;

import cn.kgc.base.SystemConstant;
import cn.kgc.entity.Users;
import cn.kgc.mapper.UsersMapper;
import cn.kgc.service.UsersService;
import cn.kgc.utils.PagedGridResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Slf4j
@Service("usersService")
@SuppressWarnings("all")
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Override
    public PagedGridResult hrList(int pageIndex, int pageSize, String companyId) {
        //查询条件
        LambdaQueryWrapper<Users> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Users::getHrInWhichCompanyId,companyId);
        wrapper.eq(Users::getRole,SystemConstant.ROLE_RECRUITERS);
        //分页条件
        Page pageParam=new Page(pageIndex,pageSize);
        //条件+分页查询
        page(pageParam,wrapper);
        //封装分页结果
        return PagedGridResult.setterPagedGrid(pageParam);
    }

    @Override
    public int getHrCount(String companyId) {
        LambdaQueryWrapper<Users> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Users::getHrInWhichCompanyId,companyId);
        return count(wrapper);
    }

    @Override
    @Transactional
    public boolean updateUserRole(String commitUserId, String companyId) {
        Users users = getById(commitUserId);
        users.setRole(SystemConstant.ROLE_RECRUITERS);
        users.setHrInWhichCompanyId(companyId);
        return updateById(users);
    }

}
